import httplib, urllib, re, time, smtplib, socket
from time import localtime, strftime

def sendEmail(downTime, upTime, msg):
	if None != upTime:
		print msg + " (" + strftime("%a, %d %b %Y %H:%M:%S", upTime) + ")"
		sServer = smtplib.SMTP('smtp.earthlink.net')
		sServer.sendmail('milkman@millcomm.com', 'milkman@millcomm.com', "To: milkman@millcomm.com\r\nFrom: milkman@millcomm.com\r\nSubject: The server came online at " + strftime("%a, %d %b %Y %H:%M:%S", localtime()) + "\r\n" + msg)
		sServer.quit()
		pServer = smtplib.SMTP('smtp-server.mn.rr.com')
		pServer.sendmail('milkman@millcomm.com', 'pottman@mn.rr.com', "To: pottman@mn.rr.com\r\nFrom: milkman@millcomm.com\r\nSubject: The server came online at " + strftime("%a, %d %b %Y %H:%M:%S", localtime()) + "\r\n" + msg)
		pServer.quit()
	else:
		print msg + " (" + strftime("%a, %d %b %Y %H:%M:%S", downTime) + ")"
		sServer = smtplib.SMTP('smtp.earthlink.net')
		sServer.sendmail('milkman@millcomm.com', 'milkman@millcomm.com', "To: milkman@millcomm.com\r\nFrom: milkman@millcomm.com\r\nSubject: The server went offline at " + strftime("%a, %d %b %Y %H:%M:%S", localtime()) + "\r\n" + msg)
		sServer.quit()
		pServer = smtplib.SMTP('smtp-server.mn.rr.com')
		pServer.sendmail('milkman@millcomm.com', 'pottman@mn.rr.com', "To: pottman@mn.rr.com\r\nFrom: milkman@millcomm.com\r\nSubject: The server went offline at " + strftime("%a, %d %b %Y %H:%M:%S", localtime()) + "\r\n" + msg)
		pServer.quit()

def testConnection():
	#connection = httplib.HTTPConnection("localhost:8080")
	connection = httplib.HTTPConnection("www.mystudentlink.com")
	headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
	params = urllib.urlencode({'USR_SCHOOL_ID': '1000', 'SCHOOLNAME': 'demo', 'submitlogin': 'Login', 'USR_PASSWORD': 'teacher', 'USRTYPE': 'TEACHER'})
	connection.request("POST", "/servlet/schoolsource.command.Pinger", params, headers)
	response = connection.getresponse().read()
	connection.close()
	return response

pingPattern = re.compile("The servlet is responding. Consider it pung.")
downTime = None

try:
	while True:
		try:
			result = testConnection()
			match = pingPattern.search(result)
			if (None == match):
				#print result
				if downTime == None:
					downTime = localtime()
					sendEmail(downTime, None, "A problem occurred. Unexpected response: " + result)
			else:
				if None != downTime:
					sendEmail(downTime, localtime(), "The server is back up")
					downTime = None
		except httplib.HTTPException, inst:
			#print "Server is down (HTTPException) at: " + strftime("%a, %d %b %Y %H:%M:%S", localtime())
			#print type(inst)
			#print inst.args
			#print inst
			if None == downTime:
				downTime = localtime()
				sendEmail(downTime, None, "An HTTPException occurred: " + str(inst))
		except socket.error, inst:
			#print "Server is down (Socket error) at: " + strftime("%a, %d %b %Y %H:%M:%S", localtime())
			if None == downTime:
				downTime = localtime()
				sendEmail(downTime, None, "A Socket error occurred: " + str(inst))
			
		time.sleep(15)
except KeyboardInterrupt:
	print "Ending loop"
		
